import wpath
from scripts.deploy.accounts import *


ORACLES = [BTCOSMedianizer, ETHOSMedianizer]


def deploy():
    # 部署硬编码的Oracle
    def test_hardcoded_config(accounts, Oracle, osm, medianizer):
        oracle = accounts[0].deploy(Oracle)
        assert oracle.OSM() == osm
        assert oracle.MEDIANIZER() == medianizer

    configs = [
        (
            BTCOSMedianizer,
            "0xf185d0682d50819263941e5f4EacC763CC5C6C42",
            "0x9B8Eb8b3d6e2e0Db36F41455185FEF7049a35CaE",
        ),
        (
            ETHOSMedianizer,
            "0x81FE72B5A8d1A857d176C3E7d5Bd2679A9B85763",
            "0x729D19f657BD0614b4985Cf1D82531c67569197B",
        ),
    ]
    for Oracle, osm, medianizer in configs:
        test_hardcoded_config(accounts, Oracle, osm, medianizer)

    # 部署Oracle
    def test_governance(accounts, gov, Oracle):
        oracle = accounts[0].deploy(Oracle)
        assert oracle.governance() == a[0]
        oracle.setGovernance(gov)
        assert oracle.governance() == gov

    for Oracle in ORACLES:
        test_governance(a, gov, Oracle)

    # 设置Oracle白名单
    def test_whitelist(a, gov, Oracle):
        oracle = a[0].deploy(Oracle)
        assert not oracle.authorized(gov)
        oracle.setAuthorized(gov)
        assert oracle.authorized(gov)
        oracle.revokeAuthorized(gov)
        assert not oracle.authorized(gov)

    for Oracle in ORACLES:
        test_whitelist(a, gov, Oracle)

    # 测试从Oracle读取价格
    def test_read(a, Oracle, func):
        oracle = a[0].deploy(Oracle)
        # getattr(oracle, func) 取出部署结果的合约对应名称的方法
        # getattr(oracle, func)() 取出合约方法并执行

        # 测试结果都出错，两个预言机读取价格read, foresight方法都出错，
        # 原因是依赖的外部合约OracleSecurityModule， UniswapAnchoredView 并没有部署
        price, osm = getattr(oracle, func)()

        assert price > 0
        assert not osm

    for Oracle in ORACLES:
        for func in ["read", "foresight"]:
            test_read(a, Oracle, func)


def config():
    # 测试从Oracle读取bud
    def test_read_bud(a, interface, OSMedianizer, func):
        oracle = OSMedianizer.at("0x82c93333e4E295AA17a05B15092159597e823e8a")
        osm = interface.OracleSecurityModule(oracle.OSM())
        assert osm.bud(oracle), "kiss first"
        reader = a[0]  # TODO: someone authorized
        assert oracle.authorized(reader)
        price, osm = getattr(oracle, func)({"from": reader})
        assert price > 0
        assert osm

    for func in ["read", "foresight"]:
        test_read_bud(a, interface, OSMedianizer, func)